package cj.web.admin.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.dao.LexiconDAO;
import cj.web.admin.domain.Lexicon;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/lexicon")
public class LexiconController extends BaseController{

	private static Logger logger = Logger.getLogger(LexiconController.class.getName());
	
	@Autowired
	private LexiconDAO lexiconDao;
	
	@Get("show")
	public String show(Model model,Lexicon lexicon) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgmgck);
		return "/views/news/lexiconList.vm";
	}

	@Post("getList")
	public String queryList(Lexicon lexicon) {
	
		List<Lexicon> arr = lexiconDao.query(lexicon);
		
		for(Lexicon obj:arr)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
		}

		JSONObject jo = new JSONObject();

		int size = lexiconDao.querySize(lexicon);

		jo.put("data", arr);
		jo.put("draw", lexicon.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgmgck);
		return "/views/news/lexiconDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Lexicon lexicon,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			lexicon.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
			lexicon.setCreateUser(returnSessionObj(request).getAccount());
			lexicon.setUpdateUser(returnSessionObj(request).getAccount());
			lexiconDao.create(lexicon);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Lexicon obj = lexiconDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgmgck);
		return "/views/news/lexiconEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Lexicon lexicon,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			lexicon.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
			lexicon.setUpdateUser(returnSessionObj(request).getAccount());
			lexiconDao.update(lexicon);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		Lexicon obj = lexiconDao.queryById(id);
		
		if(obj!=null)
		{
			lexiconDao.deleteById(id);
		}
		return "r:show";
	}
}
